package com.cqjybigscreen.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqjybigscreen.mapper.AdsCqwlCustomerMapper;
import com.cqjybigscreen.pojo.dto.BoxVolumeDTO;
import com.cqjybigscreen.pojo.dto.SummaryDateDTO;
import com.cqjybigscreen.pojo.entity.AdsCqwlCustomer;
import com.cqjybigscreen.pojo.vo.BoxTrendVO;
import com.cqjybigscreen.pojo.vo.BoxVolumeVO;
import com.cqjybigscreen.pojo.vo.EnterpriseStatisticsVO;
import com.cqjybigscreen.service.AdsCqwlCustomerService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 33618客户货物 服务实现类
 * </p>
 *
 * @author RaoChang
 */
@Service
@AllArgsConstructor
public class AdsCqwlCustomerServiceImpl extends ServiceImpl<AdsCqwlCustomerMapper, AdsCqwlCustomer> implements AdsCqwlCustomerService {
    private final AdsCqwlCustomerMapper adsCqwlCustomerMapper;

    /**
     * 获取客户分布云图
     *
     * @return List<BoxVolumeVO>
     */
    @Override
    public List<BoxVolumeVO> getCustomerDistribution(List<String> direction) {
        return adsCqwlCustomerMapper.getCustomerDistribution(direction);
    }

    /**
     * 3361企业数据统计
     *
     * @param dto direction 只需要这个值获取方向联动
     * @return EnterpriseStatisticsVO
     */
    @Override
    public EnterpriseStatisticsVO getEnterpriseStatistics(BoxVolumeDTO dto) {

        //获取企业数量和箱量总量
        BoxTrendVO enterpriseStatistics = adsCqwlCustomerMapper.getEnterpriseStatistics(dto.getDirection());
        //货物订单分类数据
        List<BoxTrendVO> orderStatistics = adsCqwlCustomerMapper.getOrderStatistics(dto.getDirection());

        //组装返回数据
        EnterpriseStatisticsVO enterpriseStatisticsVO = new EnterpriseStatisticsVO();
        enterpriseStatisticsVO.setName(enterpriseStatistics.getName());
        enterpriseStatisticsVO.setValue(enterpriseStatistics.getValue());
        enterpriseStatisticsVO.setGoodsOrderDistribution(orderStatistics);
        enterpriseStatisticsVO.setRate1("0");
        enterpriseStatisticsVO.setRate2("0");

        //计算同比环比
        SummaryDateDTO summaryDateDTO = adsCqwlCustomerMapper.getSumByDirection(dto.getDirection());
        enterpriseStatisticsVO.setRate1(calculateTongBiRate(summaryDateDTO.getSecondLastMonthTeu(),summaryDateDTO.getLastMonthTeu()));
        enterpriseStatisticsVO.setRate2(calculateTongBiRate(summaryDateDTO.getTwoYearsAgoTeu(),summaryDateDTO.getLastYearTeu()));
        return enterpriseStatisticsVO;
    }

    /**
     * 计算同比率,环比率
     */
    public String calculateTongBiRate(double lastYearValue, double thisYearValue) {
        if (lastYearValue == 0) {
            throw new IllegalArgumentException("去年的值不能为0");
        }
        double rate = (thisYearValue - lastYearValue) / lastYearValue * 100;
        // 保留两位小数
        return Math.round(rate * 100.0) / 100.0 + "%";
    }
}
